{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\6\\\\Desktop\\\\objectForPClass\\\\face_serve'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.getcwd()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\6\\\\Desktop\\\\objectForPClass\\\\face_serve'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# os.chdir(os.path.join(os.getcwd(),'face_serve'))\n",
    "os.getcwd()\n",
    "# os.listdir(os.getcwd())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python endswith() 方法用于判断字符串是否以指定后缀结尾，如果以指定后缀结尾返回True，否则返回False。\n",
    "可选参数\"start\"与\"end\"为检索字符串的开始与结束位置。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "_____________________________________________________________________________________________________________"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " - os.path.isdir用于判断某一对象(需提供绝对路径)是否为目录\n",
    " - os.path.isfile()用于判断某一对象(需提供绝对路径)是否为文件\n",
    " - os.path.abspath()获取文件的完整路径(绝对路径)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "load over\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python\n",
    "# -*- coding: utf-8 -*-\n",
    "__author__ = 'Seven'\n",
    "import os\n",
    "import numpy as np\n",
    "import cv2\n",
    " \n",
    "# 定义图片尺寸\n",
    "IMAGE_SIZE = 64\n",
    "\n",
    "# 按照定义图像大小进行尺度调整\n",
    "def resize_image(image, height=IMAGE_SIZE, width=IMAGE_SIZE):\n",
    "    top, bottom, left, right = 0, 0, 0, 0\n",
    "    # 获取图像尺寸\n",
    "    h, w, _ = image.shape\n",
    "    # 找到图片最长的一边\n",
    "    longest_edge = max(h, w)\n",
    "    # 计算短边需要填充多少使其与长边等长\n",
    "    if h < longest_edge:\n",
    "        d = longest_edge - h\n",
    "        top = d // 2\n",
    "        bottom = d // 2\n",
    "    elif w < longest_edge:\n",
    "        d = longest_edge - w\n",
    "        left = d // 2\n",
    "        right = d // 2\n",
    "    else:\n",
    "        pass\n",
    " \n",
    "    # 设置填充颜色\n",
    "    BLACK = [0, 0, 0]\n",
    "    # 对原始图片进行填充操作\n",
    "    constant = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK)\n",
    "    # 调整图像大小并返回\n",
    "    return cv2.resize(constant, (height, width))\n",
    " \n",
    "images, labels = list(), list()\n",
    "# 读取训练数据\n",
    "def read_path(path_name):\n",
    "    for dir_item in os.listdir(path_name):\n",
    "        # 合并成可识别的操作路径\n",
    "        full_path = os.path.abspath(os.path.join(path_name, dir_item))\n",
    "        # 如果是文件夹，则继续递归调用\n",
    "        if os.path.isdir(full_path):\n",
    "            read_path(full_path)\n",
    "        else:\n",
    "            if dir_item.endswith('.jpg'):\n",
    "                # print(dir_item)\n",
    "                image = cv2.imread(full_path)#此处路径斜杠问题会引起报错\n",
    "                image = resize_image(image, IMAGE_SIZE, IMAGE_SIZE)\n",
    "                images.append(image)\n",
    "                labels.append(path_name)\n",
    "#                 print(labels)#此处输出为path_name路径list,表明当前数据集合标签为此\n",
    "#     print(path_name)\n",
    "    return images, labels\n",
    "\n",
    "# 从指定路径+读取训练数据\n",
    "def load_dataset(path_name):\n",
    "    images, labels = read_path(path_name)\n",
    "    # 由于图片是基于矩阵计算的， 将其转为矩阵\n",
    "#     print(labels)\n",
    "    images = np.array(images)\n",
    "#     print(images.shape)\n",
    "    labels = np.array([0 if label.endswith('0') else 1 for label in labels])##此处根据实际改判断条件\n",
    "    return images, labels\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    images, labels = load_dataset(os.getcwd())\n",
    "    print('load over')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# from sklearn.model_selection import train_test_split\n",
    "# import random\n",
    "\n",
    "# train_images, test_images, train_labels, test_labels = train_test_split(images, labels, test_size=0.3,\n",
    "#                                                                         random_state=random.randint(0, 10))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{0, 1}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# set(labels)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
